﻿#include "Snake_playgame.h"
#include <ctime>
//初始化界面
void Snake_playgame::init() {
	Length = 500;
	Width = 750;
	hw=initgraph(Width, Length);//显示界面
	SetWindowTextA(hw, "贪吃蛇");
	setlinecolor(WHITE);
	small_snake.snake_init(Length, 500);//对蛇进行初始化
	obstacle.obs_coor();//初始化静态障碍物
	dy_obstacle.dyobs_coor();//初始化动态障碍物
	food.existfood = false;//刚开始食物不存在
	small_snake.speed = 200;
	score =0;
	for(int i = 0;i<small_snake.snake_piece;i++)
		line(Length+1+i, 0, Length + 1 + i, Length);
}
//对食物的生成进行判断
void Snake_playgame::game_foodcoor() {
	//是否和蛇重合
	for (int i = 0; i < small_snake.length; i++)
	{
		if (food.coor.x == small_snake.snake[i].x && food.coor.y == small_snake.snake[i].y)
		{
			srand((unsigned int)time(NULL));
			food.coor.x = rand() % 50 * 10;
			food.coor.y = rand() % 50 * 10;
		}
	}
	//是否和障碍物重合
	for (int i = 0; i < N; i++)
	{
		if (score>=100 && food.coor.x == obstacle.obs[i].x && food.coor.y == obstacle.obs[i].y)
		{
			srand((unsigned int)time(NULL));
			food.coor.x = rand() % 50 * 10;
			food.coor.y = rand() % 50 * 10;
		}
		if (i < Num) {
			if (score >= 200 && food.coor.x == dy_obstacle.dy_obs[i].x && food.coor.y == dy_obstacle.dy_obs[i].y)
			{
				srand((unsigned int)time(NULL));
				food.coor.x = rand() % 50 * 10;
				food.coor.y = rand() % 50 * 10;
			}
		}
	}
}
//检测是否吃到食物
bool Snake_playgame::eatfood() {
	if (food.coor.x == small_snake.snake[0].x && small_snake.snake[0].y == food.coor.y)
	{
		score += 10;
		food.existfood = true;
		small_snake.length++;
		small_snake.speed -= 10;
		if (small_snake.speed <= 100)
			small_snake.speed = 100;	
		return true;
	}
	else
	{ 
		food.existfood = false;
		return false;
	}
}
//设置障碍物
void Snake_playgame::obs_score() {
	if (score >= 100)
		obstacle.show_obs();
	if (score >=200) {
		dy_obstacle.show_dyobs();
		dy_obstacle.move_dyobs(Length);
		Sleep(small_snake.speed/4);
	}
		
}
//判断障碍物与蛇相撞
bool Snake_playgame::snake_obs() {
	if(score>=100&&score<200)
		for (int i = 0; i < N; i++) 
		{
			//判断与静态相撞
			if (obstacle.obs[i].x == small_snake.snake[0].x && obstacle.obs[i].y == small_snake.snake[0].y)
				return false;
				/*if(obstacle.obs[i].x<= small_snake.snake[0].x+ small_snake.snake_piece
				&& obstacle.obs[i].x+ obstacle.obs_shape>= small_snake.snake[0].x&&
				obstacle.obs[i].y <= small_snake.snake[0].y + small_snake.snake_piece
				&& obstacle.obs[i].y + obstacle.obs_shape >= small_snake.snake[0].y
				)*/
				//return false;
		}
	if(score>=200)
		for (int j = 0; j < Num; j++)
		{
		//判断与动态相撞
		/*if (dy_obstacle.dy_obs[j].x == small_snake.snake[0].x && dy_obstacle.dy_obs[j].y == small_snake.snake[0].y)
			return false;*/
			if (dy_obstacle.dy_obs[j].x < small_snake.snake[0].x + small_snake.snake_piece
				&& dy_obstacle.dy_obs[j].x + dy_obstacle.dy_shape > small_snake.snake[0].x &&
				dy_obstacle.dy_obs[j].y  < small_snake.snake[0].y + small_snake.snake_piece
				&& dy_obstacle.dy_obs[j].y + dy_obstacle.dy_shape > small_snake.snake[0].y
				)
			{
				
				return false;
			}
				
		}
		

	return true;
}
//判断游戏是否结束
bool Snake_playgame::is_end() {
	//包括蛇撞墙
	if (small_snake.Iswall(Length, 500) == false)
	{
		outtextxy(150, 250, _T("游戏结束"));

		return false;
	}
	//蛇撞自己
	else if (small_snake.Isme() == false)
	{
		outtextxy(150, 250, _T("游戏结束"));
		return false;
	}
	//蛇撞障碍物
	else if (score >= 100 && snake_obs() == false)
	{
		outtextxy(150, 250, _T("游戏结束"));
		return false;
	}
	else if (small_snake.length >= 100)
	{
		outtextxy(50, 250, _T("恭喜你已达最大长度  (*^▽^*)"));
		return false;
	}
		
	else
		return true;
	
}
//绘制游戏相关信息
void Snake_playgame::gameinfo(string name) {
	settextstyle(30, 0, _T("华文楷体"));//文字样式 30为字号
	//在屏幕输出当前用户
	string s = "当前用户：" + name;
	outtextxy(Length + small_snake.snake_piece + 1, 0, s.c_str());
	//在屏幕输出得分
	int text_width = textwidth("得分：");
	outtextxy(Length+small_snake.snake_piece+1, 50, "得分：");
	char num_score[20];
	sprintf_s(num_score, "%d", score);
	outtextxy(Length + small_snake.snake_piece + 1 + text_width, 50, num_score);
	//在屏幕输出长度
	outtextxy(Length + small_snake.snake_piece + 1, 100,"长度：");
	char num_length[20];
	sprintf_s(num_length, "%d", small_snake.length);
	outtextxy(Length + small_snake.snake_piece + 1 + text_width, 100, num_length);

	//在屏幕输出速度
	outtextxy(Length + small_snake.snake_piece + 1, 150, "速度：");
	char num_speed[20];
	sprintf_s(num_speed, "%d", (1000-small_snake.speed)/10);
	outtextxy(Length + small_snake.snake_piece + 1 + text_width, 150, num_speed);
	//在屏幕输出方向
	outtextxy(Length + small_snake.snake_piece + 1, 200, "方向：");
	clearrectangle(Length + small_snake.snake_piece + 1 + text_width, 200, 750, 250);
	outtextxy(Length + small_snake.snake_piece + 1 + text_width, 200, small_snake.direct);
	//在屏幕输出操作说明
	outtextxy(Length + small_snake.snake_piece + 1, 300, "操作说明：");
	//操作说明
		//使用wasd键控制
		outtextxy(Length + small_snake.snake_piece + 1, 330, "使用wasd键控制");
		//上：w
		outtextxy(Length + small_snake.snake_piece + 1, 360, "上： w");
		//下：s
		outtextxy(Length + small_snake.snake_piece + 1, 390, "下： s");
		//左：a
		outtextxy(Length + small_snake.snake_piece + 1, 420, "左： a");
		//右：d
		outtextxy(Length + small_snake.snake_piece + 1, 450, "右： d");

	
}
//开始组装
void Snake_playgame::play_game(string name) {
	//加载背景音乐
	mciSendString("open 1.mp3 alias 1 wait", NULL, 0, NULL);
	mciSendString("play 1 repeat", 0, 0, 0);
	while (true)
	{
		//先进行初始化
		init();
		//对食物坐标初始化
		food.foodcoor();
		//显示蛇
		small_snake.snake_show();

		//对于操作需要放入循环
		while (true) {
			//加入延迟相当于蛇的移动速度
			Sleep(small_snake.speed);
			//清屏
			clearrectangle(0, 0, 500, 500);
			//对食物进行判断是否需要再生成
			if (food.existfood == false)
				food.foodshow();
			//蛇的转向
			small_snake.snake_direct();
			//蛇的移动
			BeginBatchDraw();
			small_snake.snake_move();
			//蛇的显示
			small_snake.snake_show();
			//绘制游戏信息
			gameinfo(name);
			//判断是否需要障碍物
			obs_score();
			FlushBatchDraw();
			EndBatchDraw();
			//检测是否吃到食物
			if (eatfood() == true)
			{
				//判断是否需要加大难度(静态障碍物的位置开始改变)
				if (score >= 200)
					obstacle.obs_coor();
				food.foodcoor();
			}
			//检测蛇和食物是否重合
			game_foodcoor();
			//判断游戏是否结束
			if (is_end() == false)
			{
				MessageBox(hw, "按任意键重新开始！！！", "提示", MB_OK);
				_getch();
				break;
			}


		}
	}
	
}